Programming Languages Section

CoffeeScript: Just a Prettier JavaScript

Card image
Post by Amina Delali, December 13th,2021

Some Facts

CoffeeScript is a language that compiles to JavaScript . Its compilation generates a readable and pretty-printed output. The goal of the language is to simplify the JavaScript language, by defining syntactic sugars that enable you to write CoffeeScript code that is shorter and more readable than its JavaScript code equivalent. You can use the language in any type of application in which you would use JavaScript: game developpement, interactive websites ... etc.

There are 2 major versions of the language right now: CoffeeScript 1 and CoffeeScript 2. The second one compiles to a modern JavaScript code version that may not be supported in all browsers. In that case, you may want to transpile the generated modern code into an older version of JavaScript. To achieve this, you will have to install a transpiler like Babel.



How to install it

  • on Windows:

    To install CoffeeScript on Windows 10, you must have node already installed. You can install node using the Windows Installer available in this page. After that, open the command prompt, then type in one of these 2 commands:

    1. npm install --global coffeescript to install the language globally.
    2. npm install --save-dev coffeescript to install the language locally, in a project's folder.
  • on ubuntu :

    To install CoffeeScript on Ubuntu, you will also need node to be installed in your system. To do so, you can simply open your terminal and run the following commands:

    1. sudo apt update

    2. sudo apt install nodejs

    Now, you can install CoffeeScript by typing one of these two commands (as for Windows) on your terminal:

      1. npm install --global coffeescript to install the language globally.
      2. ornpm install --save-dev coffeescript to install the language locally, in a project's folder.
      3. to test the installation, just run the following command on the terminal: coffee --version

The Hello World Example

To write your first code in CoffeeScript, just follow these steps:

    1. create a new file in your home folder named hello.coffee
    2. write and save the following code in the hello.coffee file: 
      console.log("Hello World !")
    3. open the terminal from your home folder, and run the following command: coffee hello.coffee
    4. the previous command will only execute your code. But, if you want to compile it into a JavaScript code, you will have to add the --compile (or -c) option as follow: coffee --compile hello.coffee



Additional Information

For more information about the CoffeeScript language and the corresponding code, you can check the following pages:

Something to say ?

If you want to add something about the CoffeeScript language or about this post, please feel free to do it by commenting below 🙂 .